﻿// 这段 MFC 示例源代码演示如何使用 MFC Microsoft Office Fluent 用户界面 
// (“Fluent UI”)。该示例仅供参考，
// 用以补充《Microsoft 基础类参考》和 
// MFC C++ 库软件随附的相关电子文档。  
// 复制、使用或分发 Fluent UI 的许可条款是单独提供的。  
// 若要了解有关 Fluent UI 许可计划的详细信息，请访问 
// https://go.microsoft.com/fwlink/?LinkId=238214.
//
// 版权所有(C) Microsoft Corporation
// 保留所有权利。

#include "pch.h"
#include "framework.h"
#include "MainFrm.h"
#include "Resource.h"
#include "CellRichEdit.h"
#include "DBBrowser.h"
#include <uchardet.h>

class CellRichEditMenuButton : public CMFCToolBarMenuButton
{
	friend class CClassView;

	DECLARE_SERIAL(CellRichEditMenuButton)

public:
	CellRichEditMenuButton(HMENU hMenu = nullptr) noexcept : CMFCToolBarMenuButton((UINT)-1, hMenu, -1)
	{
	}

	virtual void OnDraw(CDC* pDC, const CRect& rect, CMFCToolBarImages* pImages, BOOL bHorz = TRUE,
		BOOL bCustomizeMode = FALSE, BOOL bHighlight = FALSE, BOOL bDrawBorder = TRUE, BOOL bGrayDisabledButtons = TRUE)
	{
		pImages = CMFCToolBar::GetImages();

		CAfxDrawState ds;
		pImages->PrepareDrawImage(ds);

		CMFCToolBarMenuButton::OnDraw(pDC, rect, pImages, bHorz, bCustomizeMode, bHighlight, bDrawBorder, bGrayDisabledButtons);

		pImages->EndDrawImage(ds);
	}
};

IMPLEMENT_SERIAL(CellRichEditMenuButton, CMFCToolBarMenuButton, 1)

CellRichEdit::CellRichEdit() noexcept
{
}

CellRichEdit::~CellRichEdit()
{
}

BEGIN_MESSAGE_MAP(CellRichEdit, CDockablePane)
	ON_WM_CREATE()
	ON_WM_SIZE()
	ON_WM_SETFOCUS()
	ON_WM_CONTEXTMENU()
	ON_COMMAND(ID_RICH_COPY, &CellRichEdit::OnCopy)
	ON_COMMAND(ID_RICH_CUT, &CellRichEdit::OnCut)
	ON_COMMAND(ID_RICH_PASTE, &CellRichEdit::OnPaste)
	ON_COMMAND(ID_RICH_SELECTALL, &CellRichEdit::OnSelectall)
	ON_COMMAND(ID_RICH_UNDO, &CellRichEdit::OnUndo)
	ON_COMMAND(ID_RICH_CLEAR, &CellRichEdit::OnClear)
	ON_EN_CHANGE(ID_EDIT_CELL, &CellRichEdit::OnRichEditChange)
	ON_UPDATE_COMMAND_UI(ID_BUTTON_RICHOK, &CellRichEdit::OnUpdateRichOK)
	ON_WM_DROPFILES()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CClassView 消息处理程序

int CellRichEdit::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CDockablePane::OnCreate(lpCreateStruct) == -1)
		return -1;

	CRect rectDummy;
	rectDummy.SetRectEmpty();

	// 创建视图: 
	const DWORD dwViewStyle = WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_MULTILINE | ES_WANTRETURN;
	if (!m_wndRichEdit.Create(dwViewStyle, rectDummy, this, ID_EDIT_CELL))
		return -1;
	m_wndRichEdit.SetEventMask(ENM_CHANGE | ENM_KEYEVENTS);
	m_wndToolBar.Create(this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_HIDE_INPLACE, IDR_RICHOK);
	m_wndToolBar.LoadToolBar(IDR_RICHOK, 0, 0, TRUE /* 已锁定*/);
	m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() | CBRS_TOOLTIPS | CBRS_FLYBY);
	m_wndToolBar.SetPaneStyle(m_wndToolBar.GetPaneStyle() & ~(CBRS_SIZE_DYNAMIC | CBRS_BORDER_TOP | CBRS_BORDER_BOTTOM | CBRS_BORDER_LEFT | CBRS_BORDER_RIGHT));
	m_wndToolBar.SetOwner(this);
	
	// 所有命令将通过此控件路由，而不是通过主框架路由: 
	m_wndToolBar.SetRouteCommandsViaFrame(FALSE);
	return 0;
}

void CellRichEdit::OnSize(UINT nType, int cx, int cy)
{
	CDockablePane::OnSize(nType, cx, cy);
	AdjustLayout();
}


void CellRichEdit::AdjustLayout()
{
	if (GetSafeHwnd() == nullptr)
	{
		return;
	}
	CRect rectClient;
	GetClientRect(rectClient);

	int cyTlb = m_wndToolBar.CalcFixedLayout(FALSE, TRUE).cy;

	m_wndRichEdit.SetWindowPos(nullptr, rectClient.left, rectClient.top, rectClient.Width(), rectClient.Height() - cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
	m_wndToolBar.SetWindowPos(nullptr, rectClient.left, rectClient.top + rectClient.Height() - cyTlb, rectClient.Width(), cyTlb, SWP_NOACTIVATE | SWP_NOZORDER);
}

BOOL CellRichEdit::PreTranslateMessage(MSG* pMsg)
{
	if (pMsg->wParam == 'D'&&::GetKeyState(VK_CONTROL) & 0x8000) {
		OnClear();
		return TRUE;
	}
	return FALSE;
}


void CellRichEdit::OnContextMenu(CWnd* pWnd, CPoint point)
{
	// TODO: 在此处添加消息处理程序代码
	if (m_wndRichEdit.GetStyle() & WS_DISABLED) { return; }
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_RICH, point.x, point.y, this, TRUE);
}


void CellRichEdit::OnRichEditChange()
{
	m_changed = true;
}


void CellRichEdit::OnUpdateRichOK(CCmdUI *pCmdUI)
{
	pCmdUI->Enable(m_changed);
}
